package jaegersvc

import (
	corev1 "gluenet/internal/agent/api/core/v1"
	"gluenet/utils/rpc/v2/define"
	"io"
	"net/http"
	"strings"
)

const APPGUID = "APPGUID"

func (svc *Service) JaegerService(jaegerServiceReq define.JaegerService) corev1.CommonResp {
	var data []byte
	jaegerAddr := strings.Replace(svc.jaegerCfg.Addr, APPGUID, jaegerServiceReq.Guid, 1)
	url := jaegerAddr + jaegerServiceReq.URL[strings.Index(jaegerServiceReq.URL, "/api/"):]
	svc.logger.Infof("jaeger url: %s", url)
	response, err := http.Get(url)
	if err != nil {
		svc.logger.Errorf(err.Error())
		return corev1.NewCommonFailureResp(err.Error())
	}
	data, err = io.ReadAll(response.Body)
	if err != nil {
		svc.logger.Errorf(err.Error())
		return corev1.NewCommonFailureResp(err.Error())
	}
	return corev1.NewCommonSuccessRespWithData(data)
}

func (svc *Service) JaegerOperate(jaegerServiceReq define.JaegerOperate) corev1.CommonResp {
	var data []byte
	jaegerAddr := strings.Replace(svc.jaegerCfg.Addr, APPGUID, jaegerServiceReq.Guid, 1)
	url := jaegerAddr + jaegerServiceReq.URL[strings.Index(jaegerServiceReq.URL, "/api/"):]
	svc.logger.Infof("service : %s", jaegerServiceReq.Service)
	url = strings.Replace(url, ":service", jaegerServiceReq.Service, 1)
	svc.logger.Infof("jaeger url: %s", url)
	response, err := http.Get(url)
	if err != nil {
		svc.logger.Errorf(err.Error())
		return corev1.NewCommonFailureResp(err.Error())
	}
	data, err = io.ReadAll(response.Body)
	if err != nil {
		svc.logger.Errorf(err.Error())
		return corev1.NewCommonFailureResp(err.Error())
	}
	return corev1.NewCommonSuccessRespWithData(data)
}

func (svc *Service) JaegerTraces(jaegerServiceReq define.JaegerTraces) corev1.CommonResp {
	var data []byte
	jaegerAddr := strings.Replace(svc.jaegerCfg.Addr, APPGUID, jaegerServiceReq.Guid, 1)
	url := jaegerAddr + jaegerServiceReq.URL[strings.Index(jaegerServiceReq.URL, "/api/"):]
	if jaegerServiceReq.Service != "" {
		url = url + "?service=" + jaegerServiceReq.Service
	}
	if jaegerServiceReq.Operation != "" {
		url = url + "&operation=" + jaegerServiceReq.Operation
	}
	if jaegerServiceReq.Tags != "" {
		url = url + "&tags=" + jaegerServiceReq.Tags
	}
	if jaegerServiceReq.Limit != "" {
		url = url + "&limit=" + jaegerServiceReq.Limit
	}
	if jaegerServiceReq.Start != "" {
		url = url + "&start=" + jaegerServiceReq.Start
	}
	if jaegerServiceReq.End != "" {
		url = url + "&end=" + jaegerServiceReq.End
	}
	if jaegerServiceReq.LookBack != "" {
		url = url + "&lookback=" + jaegerServiceReq.LookBack
	}
	if jaegerServiceReq.MaxDuration != "" {
		url = url + "&maxDuration=" + jaegerServiceReq.MaxDuration
	}
	if jaegerServiceReq.MinDuration != "" {
		url = url + "&minDuration=" + jaegerServiceReq.MinDuration
	}
	if jaegerServiceReq.Tags != "" {
		url = url + "&tags=" + jaegerServiceReq.Tags
	}
	svc.logger.Infof("jaeger url: %s", url)
	response, err := http.Get(url)
	if err != nil {
		svc.logger.Errorf(err.Error())
		return corev1.NewCommonFailureResp(err.Error())
	}
	data, err = io.ReadAll(response.Body)
	if err != nil {
		svc.logger.Errorf(err.Error())
		return corev1.NewCommonFailureResp(err.Error())
	}
	return corev1.NewCommonSuccessRespWithData(data)
}
